package com.aizhixin.test.question.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aizhixin.test.question.dto.QuestionOptionDTO;

@Component
public class QuestionOptionDAO {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	/*
	 * 根据抽到的题号返回对应题目的选项
	 */
	public List<QuestionOptionDTO> findByQuestionId(int x) {
		Long a=(long) x;
		final String sql = "SELECT q.OPTION,q.OPTION_CONTENT FROM t_question_option q WHERE q.QUESTION_ID=?";
		List<QuestionOptionDTO> list = jdbcTemplate.query(sql, 
				new Object[]{ x }, 
				new int[] {Types.INTEGER},
				new RowMapper<QuestionOptionDTO>() {
			public QuestionOptionDTO mapRow(ResultSet rs, 
					int rowNum) throws SQLException {
				QuestionOptionDTO test = new QuestionOptionDTO();
				test.setOptionContent(rs.getString("OPTION_CONTENT"));
				test.setOption(rs.getString("OPTION"));
				test.setQuestionID(a);

				return test;
			}
		});
		if (null != list && list.size() > 0) {
			return list;
		}
		return null;
	}

}
